<?php


require_once 'cxss.php';

//+----------------------------------------------------------------------------+
//+    CxTimer
//+----------------------------------------------------------------------------+

class CxTimer
{
    var $time =   array();  //

    /**
     *
     */
    protected $last;  //

    /**
     *
     */
    protected $count;  //

    /**
     *
     */
    function __construct ($count)
    {

        $this->count = $count;
    }

    function start ($index)
    {

        $this->last = &$this->time[$index];
        $this->last['start'] = microtime(TRUE);
    }

    function stop ($index = NULL)
    {

        $time = microtime(TRUE);
        if (isset($index))
            $this->last = &$this->time[$index];
        $this->last['stop'] = $time;
    }

    function get ($index = NULL)
    {

        if (isset($index))
            return $this->time[$index]['stop']
                - $this->time[$index]['start'];
        $result = array();
        foreach ($this->time as $index => $dummy) {
        	$result[$index] = $this->get($index);
        }
        return $result;
    }

    function test ($callback, $args = array())
    {

        if (is_callable($callback, FALSE, $index)) {
            $this->start($index);
            for ($i = $this->count; $i > 0; $i--) {
            	call_user_func_array($callback, $args);
            }
            $this->stop();
        }
        else if (is_array($callback)) {
            foreach ($callback as $cb) {
            	$this->test($cb, $args);
            }
        }
        else trigger_error(
            "Is not callabble:"
            .print_r($callable,1),
            E_USER_ERROR
        );
        return $this->get();
    }

}
?>